package com.yiwei.ydd.fragment;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.ydd.R;
import com.yiwei.ydd.activity.LoginActivity;
import com.yiwei.ydd.activity.VipPacketSecondActivity;
import com.yiwei.ydd.api.model.LeveListModel;
import com.yiwei.ydd.constant.UserInfoGlobal;
import com.yiwei.ydd.event.LoginSuccessEvent;
import com.yiwei.ydd.util.Arith;
import com.yiwei.ydd.util.Spans;
import com.yiwei.ydd.util.UI;
import com.yiwei.ydd.util.Util;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-10-10.
 */

public class VipLevelUpTipsFragment extends BaseFragment {
    @BindView(R.id.txt_recharge_num)
    TextView txtRechargeNum;
    @BindView(R.id.view_oval)
    View viewOval;
    @BindView(R.id.txt_get_num)
    TextView txtGetNum;
    @BindView(R.id.btn_level)
    SuperButton btnLevel;
    @BindView(R.id.txt_level_title_one)
    SuperButton txtLevelTitleOne;
    @BindView(R.id.txt_tips_one)
    TextView txtTipsOne;
    @BindView(R.id.txt_level_title_two)
    SuperButton txtLevelTitleTwo;
    @BindView(R.id.txt_tips_two)
    TextView txtTipsTwo;
    Unbinder unbinder;
    private Spanned textOne;
    private Spanned textTwo;
    private Spanned textThree;
    private Spanned textTwoTips;
    private Spanned textThreeTips;
    private String fragTextOne = "", fragTextTwo = "", fragTextThree = "", fragTextFour = "";
    private int x24;
    private int poi;
    private ArrayList<LeveListModel.DatasBean> totalData;
    private String fragTextStart;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_vip_level_v2;
    }

    @Override
    protected void initView() {
        x24 = getResources().getDimensionPixelSize(R.dimen.x24);
        if (getArguments() != null) {
            poi = getArguments().getInt("id", 0);
        }
        setLoginInfo();
//        EventBus.getDefault().register(this);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void setLoginInfo() {
        btnLevel.post(new Runnable() {
            @Override
            public void run() {
                if (UserInfoGlobal.isLogin()) {
                    if (poi == 0) {
                        if (UserInfoGlobal.getUserLevel() >= 1) {
                            btnLevel.setText("已获等级");
                            btnLevel.setShapeSolidColor(0xff5E524F).setUseShape();
                        } else {
                            btnLevel.setText("立即升级");
                            btnLevel.setShapeSolidColor(0xffFB5E01).setUseShape();
                        }
                    } else if (poi == 1) {
                        if (UserInfoGlobal.getUserLevel() >= 2) {
                            btnLevel.setText("已获等级");
                            btnLevel.setShapeSolidColor(0xff5E524F).setUseShape();
                        } else {
                            btnLevel.setText("立即升级");
                            btnLevel.setShapeSolidColor(0xffFB5E01).setUseShape();
                        }
                    } else if (poi == 2) {
                        if (UserInfoGlobal.getUserLevel() >= 3) {
                            btnLevel.setText("已获等级");
                            btnLevel.setShapeSolidColor(0xff5E524F).setUseShape();
                        } else {
                            btnLevel.setText("立即升级");
                            btnLevel.setShapeSolidColor(0xffFB5E01).setUseShape();
                        }
                    }


                } else {
                    btnLevel.setText("登录");
                    btnLevel.setShapeSolidColor(0xff5E524F).setUseShape();
                }
            }
        });

    }

    public void setLevelInfo(ArrayList<LeveListModel.DatasBean> datas) {
        if (datas != null && datas.size() != 0) {
            totalData = datas;
            for (int i = 0; i < datas.size(); i++) {
                LeveListModel.DatasBean item = datas.get(i);
                switch (i) {
                    case 0:
                        textOne = getTextBottom(item.sale, item.term_sale, item.uolevel_rate, item.utlevel_rate);
                        fragTextStart = item.up_money;
                        break;
                    case 1:
                        textTwo = getTextBottom(item.sale, item.term_sale, item.uolevel_rate, item.utlevel_rate);
                        textTwoTips = getTextBottomTips(item.money, item.up_money);
                        fragTextOne = item.up_money;
                        fragTextTwo = item.money;
                        break;
                    case 2:
                        textThree = getTextBottom(item.sale, item.term_sale, item.uolevel_rate, item.utlevel_rate);
                        textThreeTips = getTextBottomTipsThree(item.money, item.up_money);
                        fragTextThree = item.up_money;
                        fragTextFour = item.money;
                        break;
                }
            }

        }
        if (poi == 0) {
            txtTipsOne.setText(textOne);
            txtTipsTwo.setText("成功登录注册后，即可成为银卡会员。");
            txtLevelTitleOne.setText("银卡等级特权");
            txtLevelTitleTwo.setText("银卡等级升级说明");
            txtGetNum.setText(totalData.get(0).user_count+"人已获得");
            txtRechargeNum.setText(fragTextStart .replace(".00","")+"充值总额(元)");

        } else if (poi == 1) {
            txtTipsOne.setText(textTwo);
            txtTipsTwo.setText(textTwoTips);
            txtLevelTitleOne.setText("金卡等级特权");
            txtLevelTitleTwo.setText("金卡等级升级说明");
            if (UserInfoGlobal.getUserLevel() == 1) {
                txtRechargeNum.setText("需充值累计" + fragTextOne + "元或购买会员套餐");
                txtRechargeNum.setTextColor(0xff5E524F);
                txtGetNum.setTextColor(0xff999999);
                viewOval.setBackgroundResource(R.drawable.bg_vip_other_level);
            } else {
                txtRechargeNum.setText(fragTextOne+"充值总额(元)");
                txtRechargeNum.setTextColor(0xffFB5E01);
            }
            txtGetNum.setText(totalData.get(1).user_count+"人已获得");
        } else if (poi == 2) {
            txtTipsOne.setText(textThree);
            txtTipsTwo.setText(textThreeTips);
            txtLevelTitleOne.setText("铂金等级特权");
            txtLevelTitleTwo.setText("铂金等级升级说明");
            if (UserInfoGlobal.getUserLevel() <= 2) {
                txtRechargeNum.setText("需充值累计" + fragTextThree + "元或购买会员套餐");
                txtRechargeNum.setTextColor(0xff5E524F);
                txtGetNum.setTextColor(0xff999999);
                viewOval.setBackgroundResource(R.drawable.bg_vip_other_level);
            } else {
                txtRechargeNum.setText(fragTextThree+"充值总额(元)");
                txtRechargeNum.setTextColor(0xffFB5E01);
                txtGetNum.setTextColor(0xffFB5E01);
            }
            txtGetNum.setText(totalData.get(2).user_count+"人已获得");
        }

//        fragment.setInfoData(fragTextOne, fragTextTwo, fragTextThree, fragTextFour);
        setLoginInfo();
    }

    private Spanned getTextBottom(String sale, String term_sale, String uolevel_rate, String utlevel_rate) {
        return Spans.with(this).font("1.即时充值可享受最低", x24, 0xff333333).
                font(Float.valueOf(sale) / 10 + "", x24, 0xffFB5E01).style(Typeface.BOLD).
                font("折优惠；\n2.购买加油套餐最多可享受赠送", x24, 0xff333333).
                font(term_sale + "%", x24, 0xffFB5E01).style(Typeface.BOLD).
                font("的充值返现；\n3.分享推荐拿佣金提成：一级奖励", x24, 0xff333333).
                font(uolevel_rate + "%", x24, 0xffFB5E01).style(Typeface.BOLD).
                font("，二级奖励", x24, 0xff333333).
                font(utlevel_rate + "%", x24, 0xffFB5E01).style(Typeface.BOLD).
                font("。", x24, 0xff333333).
                build();
    }

    private Spanned getTextBottomTips(String money, String up_money) {
        return Spans.with(this).font("满足其中一个条件即可成为金卡会员：\n1.购买会员升级套餐，需要支付 ", x24, 0xff333333).
                font(money, x24, 0xffFB5E01).style(Typeface.BOLD).
                font("元；\n2.在本平台进行油卡即时充值或购买加油套餐实际付款累计总额达到 ", x24, 0xff333333).
                font(up_money, x24, 0xffFB5E01).style(Typeface.BOLD).
                font("元整(折扣金额不算入内)。", x24, 0xff333333).
                build();
    }

    private Spanned getTextBottomTipsThree(String money, String up_money) {
        String need_money = Arith.sub(Double.valueOf(money), Double.valueOf(fragTextTwo)) + "";
        return Spans.with(this).font("满足其中一个条件即可成为铂金会员：\n1.购买会员升级套餐，需要支付 ", x24, 0xff333333).
                font(money, x24, 0xffFB5E01).style(Typeface.BOLD).
                font("元；\n2.当您的当前身份是金卡会员时，购买铂金会员升级套餐，则只需要支付差价 ", x24, 0xff333333).
                font(need_money, x24, 0xffFB5E01).style(Typeface.BOLD).
                font("元(" + money.replace(".0", "").replace(".00", "") + "元-" + fragTextTwo.replace(".0", "").replace(".00", "") + "元=" + need_money.replace(".0", "").replace(".00", "") + "元);", x24, 0xff333333).
                font("\n3.在本平台进行油卡即时充值或购买加油套餐实际付款累计总额达到 ", x24, 0xff333333).
                font(up_money, x24, 0xffFB5E01).style(Typeface.BOLD).
                font("元整(折扣金额不算入内)。", x24, 0xff333333).
                build();
    }
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onEvent(LoginSuccessEvent event) {
//        setLoginInfo();
//    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @OnClick(R.id.btn_level)
    public void onViewClicked() {
        if (UI.toString(btnLevel).equals("立即升级")) {
            Intent intent = new Intent(getContext(), VipPacketSecondActivity.class);
            intent.putParcelableArrayListExtra("data",totalData);
            startActivity(intent);
            getActivity().overridePendingTransition(R.anim.right_enter, R.anim.left_out);
//            Util.startActivity(getContext(), VipPacketSecondActivity.class);
        } else if (UI.toString(btnLevel).equals("登录")) {
            Util.startActivity(getContext(), LoginActivity.class);
        }
    }
}
